package com.demo2do.fms.admin.service.data.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.data.OilPriceDao;
import com.demo2do.fms.admin.entity.report.data.OilPriceReportSearch;
import com.demo2do.fms.admin.handler.support.FmsJdbcSupport;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.data.OilPriceService;
import com.demo2do.fms.entity.data.cost.OilPrice;

/**
 * 
 * @author Ysy
 *
 */
@Service("oilPriceService")
public class OilPriceServiceImpl extends DataServiceImpl<OilPrice> implements OilPriceService{
    
    @Dao
    private OilPriceDao oilPriceDao;
    
    @Autowired
    private FmsJdbcSupport fmsJdbcSupport;
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.OilPriceService#count(java.util.List)
     */
    public int count(List<Long> orgIds) {
        
        //if orgIds is null, return 0
        if(CollectionUtils.isEmpty(orgIds))
            return 0;
            
        return oilPriceDao.count(orgIds);
        
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.OilPriceService#list(java.util.List, com.demo2do.core.entity.page.Page)
     */
    public List<OilPrice> list(List<Long> orgIds, Page page) {
        List<OilPrice> result = oilPriceDao.list(orgIds, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.OilPriceService#load(java.lang.Long, java.lang.Long)
     */
    public OilPrice getOilPrice(Long orgId, Long oilType) {
        return oilPriceDao.getOilPrice(orgId, oilType);
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.OilPriceService#create(java.lang.Long, com.demo2do.fms.entity.data.cost.OilPrice)
     */
    public void create( Long operator , OilPrice oilPrice) {
        genericDaoSupport.save(oilPrice.init().operator(operator));
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.data.OilPriceService#exportAll(java.util.List)
     */
    public List<Map<String, Object>> exportAll(OilPriceReportSearch search) {
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return fmsJdbcSupport.queryForList(sentence, parameters, search.buildRowMapper());
    }

}
